﻿//2011-01-11    Initial                     By 應翰

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//Using Namespace
using System.Data;
using System.Data.SqlClient;
using com.chttl.ienc.bean;
using java.util;

namespace DataAccess
{
    public class WebService
    {
        string strUpdateTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
        
        IENCentreOrganization.OrganizationService svc = new IENCentreOrganization.OrganizationService();
        
        #region public DataTable QuerySiteMap(string strSiteID)
        public string Test(string temp)
        {
            string strtemp = "";
            Result result = ToolBox.getResultFromResponse(svc.getAllCustomers0().@return);
            //Console.WriteLine("return code: " + result.getCode());
            //Console.WriteLine("return msg: " + result.getMsg());
            foreach (Customer customer in ((DataObjectArray)result.getUserData()).getData())
            {
                //Console.WriteLine("id=" + customer.getId() + ", refId=" + customer.getRefId() + ", name=" + customer.getName());
                strtemp = customer.getId() + "//" + customer.getRefId() + "//" + customer.getName();
            }

            return strtemp;
        }
        #endregion

        public string getCustomerByID(int id)
        {
            string res = "";

            res = svc.getCustomer(1, true);
            Result result = ToolBox.getResultFromResponse(res);
            Customer customer = (Customer)((DataObject)result.getUserData());
            res = customer.getName();

            return res;
        }


        /*
         * 
         * 「設備履歷」->「組織群組管理」
         * 
         */
        #region
        public OrgGroup getOrgGroupByPath1(string path)
        {
            string res = "";

            res = svc.getOrgGroupByPath1(path);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            OrgGroup orgGroup = (OrgGroup)(result.getUserData());
            return orgGroup;
        }

        public OrgGroup createOrgGroup6(string parent, string refId, string name,
                                        string category, string type, string description)
        {
            string res = "";

            res = svc.createOrgGroup6(parent, refId, name, category, type, description);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            OrgGroup orgGroup = (OrgGroup)(result.getUserData());
            return orgGroup;
        }

        public DataObjectArray getOrgGroups3(string parent, string category, string type)
        {

            string res = "";

            res = svc.getOrgGroups3(parent, category, type);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            DataObjectArray doa = (DataObjectArray)(result.getUserData());

            return doa;
        }

        public DataObjectArray getOrgGroups4(string parent, string category, string type,
                                             int deviceTypeId)
        {

            string res = "";

            bool deviceTypeIdSpecified = deviceTypeId==-1 ? false : true;
            res = svc.getOrgGroups4(parent, category, type, deviceTypeId, deviceTypeIdSpecified);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            DataObjectArray doa = (DataObjectArray)(result.getUserData());

            return doa;
        }

        public OrgGroup modifyOrgGroup1(OrgGroup group)
        {

            string res = "";

            res = svc.modifyOrgGroup1(BeanUtil.marshallBeanToXML(group));
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            OrgGroup orgGroup = (OrgGroup)(result.getUserData());
            return orgGroup;
        }

        public bool removeOrgGroup1(string path)
        {

            string res = "";

            res = svc.removeOrgGroup1(path);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() == ReturnCode.SUCCESS)
                return true;
            else
                return false;
        }
        #endregion

        /*
         * 
         * 「設備履歷」->「控制器管理」
         * 
         */
        #region
        public Controller createController1(Controller controller)
        {
            string res = "";

            res = svc.createController1(BeanUtil.marshallBeanToXML(controller));
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            Controller returnController = (Controller)(result.getUserData());
            return returnController;
        }

        public Controller getController1(int controllerId)
        {
            string res = "";

            res = svc.getController1(controllerId, true);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            Controller returnController = (Controller)(result.getUserData());
            return returnController;
        }

        public DataObjectArray getControllersByControlServerId1(int controlServerId)
        {
            string res = "";

            res = svc.getControllersByControlServerId1(controlServerId, true);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            DataObjectArray doa = (DataObjectArray)(result.getUserData());

            return doa;
        }

        public Controller modifyController1(Controller controller)
        {
            string res = "";

            res = svc.modifyController1(BeanUtil.marshallBeanToXML(controller));
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            Controller returnController = (Controller)(result.getUserData());
            return returnController;
        }

        public bool removeController1(int controllerId)
        {
            string res = "";

            res = svc.removeController1(controllerId, true);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() == ReturnCode.SUCCESS)
                return true;
            else
                return false;
        }
        #endregion


        /*
         * 
         * 「設備履歷」->「設備管理」
         * 
         */
        #region
        public Device createDevice1(Device device)
        {
            string res = "";

            res = svc.createDevice1(BeanUtil.marshallBeanToXML(device));
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            Device returnDevice = (Device)(result.getUserData());
            return returnDevice;
        }

        public Device createDevice2(string parent, Device device)
        {
            string res = "";

            res = svc.createDevice2(parent, BeanUtil.marshallBeanToXML(device));
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            Device returnDevice = (Device)(result.getUserData());
            return returnDevice;
        }

        public Device getDevice1(int deviceId)
        {
            string res = "";

            res = svc.getDevice1(deviceId, true);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            Device returnDevice = (Device)(result.getUserData());
            return returnDevice;
        }

        public DataObjectArray getDevicesByControllerId1(int controllerId)
        {
            string res = "";

            res = svc.getDevicesByControllerId1(controllerId, true);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            DataObjectArray doa = (DataObjectArray)(result.getUserData());

            return doa;
        }

        public Device modifyDevice1(Device device)
        {
            string res = "";

            res = svc.modifyDevice1(BeanUtil.marshallBeanToXML(device));
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            Device returnDevice = (Device)(result.getUserData());
            return returnDevice;
        }

        public bool removeDevice1(int deviceId)
        {
            string res = "";

            res = svc.removeDevice1(deviceId, true);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() == ReturnCode.SUCCESS)
                return true;
            else
                return false;
        }

        #endregion


        /*
         * 
         * 「設備履歷」->「監控點管理」
         * 
         */
        #region
        public Tag createTag1(Tag tag)
        {
            string res = "";

            res = svc.createTag1(BeanUtil.marshallBeanToXML(tag));
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            Tag returnTag = (Tag)(result.getUserData());
            return returnTag;
        }

        public DataObjectArray createVirtualTag1(VirtualTag virtualTag)
        {
            string res = "";

            res = svc.createVirtualTag1(BeanUtil.marshallBeanToXML(virtualTag));
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            DataObjectArray doa = (DataObjectArray)(result.getUserData());

            return doa;
        }

        public Tag getTag1(int tagId)
        {
            string res = "";

            res = svc.getTag1(tagId, true);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            Tag returnTag = (Tag)(result.getUserData());
            return returnTag;
        }

        public DataObjectArray getTagsByDeviceId1(int deviceId)
        {
            string res = "";

            res = svc.getTagsByDeviceId1(deviceId, true);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            DataObjectArray doa = (DataObjectArray)(result.getUserData());

            return doa;
        }

        public Tag modifyTag1(Tag tag)
        {
            string res = "";

            res = svc.modifyTag1(BeanUtil.marshallBeanToXML(tag));
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            Tag returnTag = (Tag)(result.getUserData());
            return returnTag;
        }

        public VirtualTag modifyVirtualTag1(VirtualTag virtualTag)
        {
            string res = "";

            res = svc.modifyVirtualTag1(BeanUtil.marshallBeanToXML(virtualTag));
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() != ReturnCode.SUCCESS)
                return null;

            VirtualTag returnVirtualTag = (VirtualTag)(result.getUserData());
            return returnVirtualTag;
        }

        public bool removeTag1(int tagId)
        {
            string res = "";

            res = svc.removeTag1(tagId, true);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() == ReturnCode.SUCCESS)
                return true;
            else
                return false;
        }

        public bool removeVirtualTag1(int virtualTagId)
        {
            string res = "";

            res = svc.removeVirtualTag1(virtualTagId, true);
            Result result = ToolBox.getResultFromResponse(res);

            if (result.getCode() == ReturnCode.SUCCESS)
                return true;
            else
                return false;
        }
        #endregion


        // 20100221 appended
        /*************************************************************************************/
        IENCentreRealtime.RealtimeService svcReal = new IENCentreRealtime.RealtimeService();

        public TagValue GetRealtimeTagValue(int intDeviceID, string strAttribute, int intTagID)
        {
            try
            {
                String res = svcReal.getTagValues2(intDeviceID, true, strAttribute);
                Result result = ToolBox.getResultFromResponse(res);

                DataObject[] objAry = ((DataObjectArray)result.getUserData()).getData();
                TagValue tvRet = null;

                foreach (TagValue tv in objAry)
                {
                    //Console.WriteLine("Tag=" + tv.getId() + " " + tv.getValue() + tv.getValueAlias());
                    int intID = tv.getId();
                    if (tv.getId() == intTagID)
                        tvRet = tv;
                }

                return tvRet;

            }
            catch
            {
                return null;
            }

        }

        public DataObject[] GetRealtimeTagValue(int intDeviceID, string strAttribute)
        {
            try
            {
                String res = svcReal.getTagValues2(intDeviceID, true, strAttribute);
                Result result = ToolBox.getResultFromResponse(res);

                DataObject[] objAry = ((DataObjectArray)result.getUserData()).getData();
                //TagValue tvRet = null;

                //foreach (TagValue tv in objAry)
                //{
                //    //Console.WriteLine("Tag=" + tv.getId() + " " + tv.getValue() + tv.getValueAlias());
                //    int intID = tv.getId();
                //    if (tv.getId() == intTagID)
                //        tvRet = tv;
                //}

                return objAry;

            }
            catch
            {
                return null;
            }

        }


        public Boolean SetTagValue(int DeviceID, int TagID, double dblValue)
        {
            try
            {
                String res = svcReal.setTagValue3(DeviceID, true, TagID, true, dblValue, true);
                Result result = ToolBox.getResultFromResponse(res);
                if (result.getMsg() != "Success")
                {
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }


        }


        /*************************************************************************************/

        // 20100616 appended
        /*************************************************************************************/
        public DataObject[] GetRealtimeTagValue(int[] tagIDs)
        {
            try
            {
                String res = svcReal.getTagValuesByTagIds1(tagIDs);
                Result result = ToolBox.getResultFromResponse(res);

                DataObject[] objAry = ((DataObjectArray)result.getUserData()).getData();
                //TagValue tvRet = null;

                //foreach (TagValue tv in objAry)
                //{
                //    //Console.WriteLine("Tag=" + tv.getId() + " " + tv.getValue() + tv.getValueAlias());
                //    int intID = tv.getId();
                //    if (tv.getId() == intTagID)
                //        tvRet = tv;
                //}

                return objAry;

            }
            catch (Exception e)
            {
                return null;
            }

        }

        /*************************************************************************************/
    }
}

